<?php
class Admin_AdminUserController extends Zendvn_Controller_Action{
	
	
	private $_paning = array('rowCount'=> 10, 'pageRange'=> 3);
	
	private $_dirUpload;
	
	protected $_config;
	
	public function init(){
		parent::init();		
		$this->_templateObj->setPath(TEMPLATE_PATH . '/admin/white');		
		$this->_templateObj->load();	
		
		$ss = new Zend_Session_Namespace($this->_ssNamespace);
		if(empty($ss->col)){
			$ss->col = 'u.id';
			$ss->order = 'DESC';
		}
		$ss->setExpirationSeconds(600);
		$this->_arrParam['arrFilter'] = $ss->getIterator();
		
		$pageNumber = $this->_request->getParam('page',1);
		$this->_paning['page'] 			= $pageNumber;
		$this->_paning['rowCount'] 		= $this->_configObj->getSite('list_limit');		
		$this->_paning['pageRange'] 	= 3;				
		$this->_arrParam['panigator'] 	= $this->_paning;
		$this->_config  = new Zendvn_Controller_Config(array('module'=>$this->_arrParam['module'],
													 	  'fileConfig'=>'user.ini'));($this->_arrParam);
		$this->view->arrParam = $this->_arrParam;
	}
	
	public function indexAction(){
	
		$this->view->Title = $this->view->translate('Manager') . ' : '
								. ' : ' .  $this->view->translate('Users') . ' : '
							 	. ' : ' .  $this->view->translate('List');
		$this->view->headTitle($this->view->Title,true);
		
		$tblGroup 					= new Zendvn_Model_Table_Group();
		$this->view->sblGroup 		= $tblGroup->itemInSelect();
		
		$tblUser 					= new Zendvn_Model_Table_User();
		$this->view->Items 			= $tblUser->run($this->_arrParam, array('run'=>'list-item'));
		$totalItem 					= $tblUser->run($this->_arrParam, array('run'=>'total-item'));
		$this->view->panigator 		= Zendvn_Paginator::create($this->_arrParam['panigator'], $totalItem);		
		$this->view->slbSearchFiels = $tblUser->run($this->_arrParam, array('run'=>'search-fields'));
		
	}

	public function addAction(){
		$this->view->Title = $this->view->translate('Manager')  . ' : '
								. ' : ' .  $this->view->translate('Users') . ' : '
							 	. " : " . $this->view->translate('Add');
		$this->view->headTitle($this->view->Title,true);
		$this->view->headLink()->appendStylesheet(PUBLIC_URL . '/css/jquery-ui-1.9.2.css','screen');
		
		$tblGroup 					= new Zendvn_Model_Table_Group();
		$this->view->sblGroup 		= $tblGroup->itemInSelect();
		$this->view->sblExtGroup 	= $tblGroup->itemInSelect(null, 'ext-group');
		
		$tblUser = new Zendvn_Model_Table_User();
		
		if($this->_request->isPost()){
			$validator = new Admin_Form_ValidateUser($this->_arrParam);
			if($validator->isVaild()){
				$this->view->errors = $validator->getMessageErrors();
				$this->view->Item = $validator->getData();
			}else{
				$this->_helper->flashMessenger->addMessage(array('success'=>'The update was successful'));
				$data =  $validator->getData();
				
				$tblUser->saveItem($data,array('task'=>'admin-add'));
				if($this->_arrParam['type'] == 'back'){
					$this->_redirect($this->_mainAction);
				}else{
					$this->_redirect($this->_currentController . '/' . $this->_arrParam['action']);
				}
				$this->_helper->viewRenderer->setNoRender();
			}
		}
		
		$this->_helper->viewRenderer('form');
	}
	
	public function editAction(){
		$this->view->Title = $this->view->translate('Manager')  . ' : '
								. ' : ' .  $this->view->translate('Users') . ' : '
							 	. " : " . $this->view->translate('Edit');
		$this->view->headTitle($this->view->Title,true);
		
		$this->view->headLink()->appendStylesheet(PUBLIC_URL . '/css/jquery-ui-1.9.2.css','screen');
		
		$tblUser = new Zendvn_Model_Table_User();
		$this->view->Item = $tblUser->getItem($this->_arrParam);
		
		$tblGroup = new Zendvn_Model_Table_Group();
		$this->view->sblGroup = $tblGroup->itemInSelect();
		$this->view->groupID = $tblGroup->getDefaultGroup($this->_arrParam['id'],'id');
		$this->view->sblExtGroup = $tblGroup->itemInSelect(null,'ext-group');
		
		$this->view->extGroupID = $tblGroup->getExtGroup($this->_arrParam['id'],'id');
		
		$this->view->config = $this->_config;
		
		if($this->_request->isPost()){
			$validator = new Admin_Form_ValidateUser($this->_arrParam);
			if($validator->isVaild()){
				$this->view->errors = $validator->getMessageErrors();				
				$this->view->Item = $validator->getData();		
			}else{
				$this->_helper->flashMessenger->addMessage(array('success'=>'The update was successful'));
				$data =  $validator->getData();		
				
				$tblUser = new Zendvn_Model_Table_User();
				$tblUser->saveItem($data,array('task'=>'admin-edit'));
				if($this->_arrParam['type'] == 'back'){
					$this->_redirect($this->_mainAction);
				}else{
					$this->_redirect($this->_currentController 
									 . '/' . $this->_arrParam['action'] 
									 . '/id/' . $this->_arrParam['id']);
				}
				$this->_helper->viewRenderer->setNoRender();
			}
		}
		$this->_helper->viewRenderer('form');	
	}
	
	public function infoAction(){
		$this->view->Title = $this->view->translate('Manager') . ' : '
							. ' : ' .  $this->view->translate('Users') . ' : '
							. " : " . $this->view->translate('Info');
		$this->view->headTitle($this->view->Title,true);
		
		$tblUser = new Zendvn_Model_Table_User();
		$this->view->Item = $tblUser->getItem($this->_arrParam,array('task'=>'info'));
		$this->view->config = $this->_config;
	}
	
	public function deleteAction(){
		$this->view->Title = "Member :: Member manager :: Delete";
		$this->view->headTitle($this->view->Title,true);		
		$tblUser = new Zendvn_Model_Table_User();
		
		if($this->_arrParam['type'] == 'multi'){
			$tblUser->deleteItem($this->_arrParam, array('task'=>'multi'));
		}else{
			$tblUser->deleteItem($this->_arrParam);
		}
		$this->_helper->flashMessenger->addMessage(array('success'=>'The delete was successful'));
		$this->_redirect($this->_mainAction);
		$this->_helper->viewRenderer->setNoRender();
	}
	
	public function statusAction(){
		$tblUser = new Zendvn_Model_Table_User();
	
		if($this->_arrParam['type'] == 'multi'){
			$tblUser->statusItem($this->_arrParam,array('task'=>'multi'));
		}else{
			$tblUser->statusItem($this->_arrParam);
		}
		
		$this->_redirect($this->_mainAction);
		$this->_helper->viewRenderer->setNoRender();
		$this->_helper->layout->disableLayout();
	}
	
	public function filterAction(){
		
		$ss = new Zend_Session_Namespace($this->_ssNamespace);
		
		if($this->_arrParam['type'] == 'search'){
			$ss->keyword = $this->_arrParam['keyword'];
			$ss->searchField = $this->_arrParam['search-field'];
		}
		
		if($this->_arrParam['type'] == 'clear'){
			$ss->keyword = '';
			$ss->searchField = '';
		}
		
		if($this->_arrParam['type'] == 'filter'){
			$ss->status 		= $this->_arrParam['status'];
			$ss->group_id 		= $this->_arrParam['group_id'];
		}
		
		if($this->_arrParam['type'] == 'sort'){
			$ss->col = $this->_arrParam['col'];
			$ss->order = $this->_arrParam['order'];
		}
		
		$this->_redirect($this->_mainAction);
		$this->_helper->viewRenderer->setNoRender();
	}
	
	public function removePicAction(){
		$config = new Zendvn_Controller_Config(array('module'=>$this->_arrParam['module'],
													 	  'fileConfig'=>'user.ini'));
		
		$upload = new Zendvn_File_Upload();
		$tblUser = new Zendvn_Model_Table_User();
		
		$avatarConfig = $config->getConfig('config');
		$upload->remove($avatarConfig['uploadDir'],$this->_arrParam['file']);
		$upload->remove($avatarConfig['thumbDir'],$this->_arrParam['file']);			
		$upload->remove($avatarConfig['thumbDir1'],$this->_arrParam['file']);			
		$upload->remove($avatarConfig['thumbDir2'],$this->_arrParam['file']);			
		$upload->remove($avatarConfig['thumbDir3'],$this->_arrParam['file']);			
		$tblUser->update(array('avatar'=>''), 'id = ' . $this->_arrParam['id']);
							
		$this->_helper->viewRenderer->setNoRender();
		$this->_helper->layout()->disableLayout();
	}
}